﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Net5ApiAndEfMySql.API.Controllers;
using Net5ApiAndEfMySql.DtoModel;
using VueAndNetCoreEfApi.DtoModel.SystemAdmin;
using VueAndNetCoreEfApi.IService.SystemAdmin;
using VueAndNetCoreEfApi.PoModel.SystemAdmin;

namespace VueAndNetCoreEfApi.Controllers
{
    public class OwnerController : BaseApiController
    {

        private IOwnerService _iOwnerService;

        public OwnerController(IOwnerService iOwner)
        {
            _iOwnerService = iOwner;
        }


        
        
        public ResultModel GetOwnerList(SearchModel m)
        {
            ResultModel result = new ResultModel();
            if (m == null)
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {

                var data = _iOwnerService.GetPageList(m);


                data.draw = m.draw;
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;

        }

        //添加
        public ResultModel OwnerAdd(OwnerDto model)
        {
            ResultModel result = new ResultModel();

            try
            {
                var resultData = _iOwnerService.OwnerAdd(model);
                if (resultData)
                {
                    result.code = ResultCode.Ok;
                    result.data = resultData;
                    result.mess = "成功!";
                }
                else
                {
                    result.code = ResultCode.Fail;
                    result.data = resultData;
                    result.mess = "添加失败!";
                }
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;

          

        }

        //删除

        public ResultModel DelOwnerById(int ow_id)
        {
            ResultModel result = new ResultModel();

            try
            {
                var resultData = _iOwnerService.DelOwnerById(ow_id);
                if (resultData)
                {
                    result.code = ResultCode.Ok;
                    result.data = resultData;
                    result.mess = "成功!";
                }
                else
                {
                    result.code = ResultCode.Fail;
                    result.data = resultData;
                    result.mess = "添加失败!";
                }
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
          

        }

        public ResultModel GetOwnerById(int ow_id)
        {

            ResultModel result = new ResultModel();
            var model = _iOwnerService.GetOwnerById(ow_id);

            if (model == null)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            else
            {
                result.code = ResultCode.Ok;
                result.data = model;
                result.mess = "成功!";
            }

            return result;
        }


        //修改
        public bool OwnerUpdate(OwnerPo model)
        {
            return _iOwnerService.OwnerUpdate(model);

        }

    }
}
